package com.lianniu.mgr.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.dao.IPopChannelDao;
import com.lianniu.mgr.cms.model.PopChannelVo;
import com.lianniu.mgr.cms.service.IPopChannelServie;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;

@Service
public class PopChannelServiceImpl implements IPopChannelServie {

	private final static Logger logger = Logger.getLogger(PopChannelServiceImpl.class);
	  
	@Resource
	IPopChannelDao popChannelDao;
	
	@Override
	public BaseResult insertPopChannel(PopChannelVo curPopChannelVo) {
		// TODO Auto-generated method stub
	    logger.info("insertPopChannel begin");
	    logger.info("insertPopChannel: input data check begin");
	    logger.info("insertPopChannel curPopChannelVo:" +JSON.toJSONString(curPopChannelVo));	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curPopChannelVo.getChannelId())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }	    
	    if (StringUtil.isEmpty(curPopChannelVo.getType())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }  
	    if (StringUtil.isEmpty(curPopChannelVo.getSupplyProductId())) {
	    	  baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		  }  
	    if (StringUtil.isEmpty(curPopChannelVo.getPositions())) {
		      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }
	    if (StringUtil.isEmpty(curPopChannelVo.getPicUrl())) {
	    	  baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		  }  
	    if (StringUtil.isEmpty(curPopChannelVo.getPicLink())) {
	    	  baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		  }  
	    if (StringUtil.isEmpty(curPopChannelVo.getSort())) {
	    	curPopChannelVo.setSort("0");
		  }   
	    
	    logger.info("insertPopChannel: input data check end");	    
	    return BaseResult.success(popChannelDao.insertPopChannel(curPopChannelVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
		 // TODO Auto-generated method stub
		   logger.info("deleteById begin");
		   logger.info("deleteById Id:" +Id);
						  
		   return BaseResult.success(popChannelDao.deleteById(Id));
	}

	@Override
	public BaseResult deletePopChannelBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deletePopChannelBatch begin");
	    logger.info("deletePopChannelBatch Ids:" +id.toString());
	    logger.info("deletePopChannelBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteChannelBatch: input data check end");
	    
	    return BaseResult.success(popChannelDao.deletePopChannelBatch(id));
	}

	@Override
	public BaseResult updateById(PopChannelVo curPopChannelVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curPopChannelVo:" +JSON.toJSONString(curPopChannelVo));
	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curPopChannelVo.getChannelId())) {
	      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curPopChannelVo.getPicLink())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curPopChannelVo.getPicUrl())) {
		      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }
	    if (StringUtil.isEmpty(curPopChannelVo.getPositions())) {
		      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }	       
	    if (StringUtil.isEmpty(curPopChannelVo.getSort())) {
	    	curPopChannelVo.setSort("0");
		}  
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(popChannelDao.updateById(curPopChannelVo));
	}

	@Override
	public BaseResult selectByPopChannelForPage(PopChannelVo curPopChannelVo,PaginationInfo paginationInfo) {
		// TODO Auto-generated method stub
	    logger.info("selectByPopChannelForPage begin.");
	    if (!StringUtil.isEmpty(curPopChannelVo.getChannelName())) {
	    	curPopChannelVo.setChannelName(curPopChannelVo.getChannelName().replace(" ", ""));
	    }
	    logger.info("selectByPopChannelForPage Title:" +curPopChannelVo.getChannelName());
	
	    logger.info("selectByPopChannelForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curPopChannelVo.setPaginationInfo(paginationInfo);
	    List<PopChannelVo> list=popChannelDao.selectByPopChannelForPage(curPopChannelVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByPopChannelForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult selectById(PopChannelVo curPopChannelVo) {
		// TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curPopChannelVo.getId());
	  
	    return BaseResult.success(popChannelDao.selectById(curPopChannelVo.getId()));
	}

}
